package com.yuncheng.sms.filter;

import com.yuncheng.common.utils.IPUtils;
import com.yuncheng.sms.service.ApiSignManager;
import com.yuncheng.sms.service.LoginSysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * LoginSmsCodeFilter
 *
 * @date 2023/4/10
 */
@Component
@Order(Integer.MIN_VALUE + 10)
public class LoginSmsCodeFilter extends OncePerRequestFilter {

    @Autowired
    private LoginSysConfigService loginSysConfigService;

    @Autowired
    private ApiSignManager apiSignManager;

    @Value("${login_post_url:/api/system/login2}")
    private String loginPostUrl;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //如果是 登录请求 则执行
        if ((request.getMethod().equalsIgnoreCase("post") &&
                loginPostUrl.equals(request.getServletPath()))) {
            String ipAddr = IPUtils.getIpAddr(request);
            int smscode = loginSysConfigService.checkPcNeedSmsCode(ipAddr);
            if (apiSignManager.isNeedSmscodeServer(ipAddr)) {
                // 是需要发送验证码的内网服务器IP，则根据islan参数决定是否校验验证码
                if (LoginSysConfigService.LOGIN_ISLAN_NOVERIFY.equals(request.getParameter(LoginSysConfigService.LOGIN_ISLAN_KEY))) {
                    //接收到内网无校验标志，不需要校验验证码
                    smscode = LoginSysConfigService.NO_SMSCODE;
                } else {
                    // 其他情况都需要校验验证码
                    smscode = LoginSysConfigService.NEED_SMSCODE;
                }
            }

            boolean hasSign = false;
            String usernmae = loginSysConfigService.obtainUserName(request);
            String sendid = loginSysConfigService.obtainSendId(request);
            if (LoginSysConfigService.NO_SMSCODE == smscode
                    && StringUtils.hasText(usernmae) && StringUtils.hasText(sendid)) {
                if (loginSysConfigService.hasSignCode(usernmae, sendid)) {
                    // sendid是内网服务器请求发送的，则必须校验短信验证码
                    smscode = LoginSysConfigService.NEED_SMSCODE;
                    hasSign = true;
                }
            }

            // 如果登录需要短信验证码
            if (LoginSysConfigService.NEED_SMSCODE == smscode) {
                if (!loginSysConfigService.checkSmsVerifyCode(request, response)) {
                    // 校验验证码失败，直接退出
                    return;
                } else if (hasSign) {
                    // 校验验证码成功，去掉signcode标记
                    loginSysConfigService.removeSignCode(usernmae, sendid);
                }
            }
        }
        filterChain.doFilter(request, response);
    }
}
